package com.cindata

import com.cindata.dictionary.*
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Transactional
@Secured(['isAuthenticated()'])
class ContactRiskWarningDispositionTypeController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index()
    {
        def max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        def offset = params.offset ? params.offset.toInteger() : 0

        def code = params['code']
        def name = params['name']

        def sql = "from ContactRiskWarningDispositionType where 1=1 "
        if (code)
        {
            sql += " and code like '%${code}%'"
        }
        if (name)
        {
            sql += " and name like '%${name}%'"
        }

        def countSql = "select count(id) " + sql
        sql += " order by name desc "

        def contactRiskWarningDispositionTypeList = ContactRiskWarningDispositionType.executeQuery(sql, [max: max, offset: offset])
        def contactRiskWarningDispositionTypeCount = ContactRiskWarningDispositionType.executeQuery(countSql)
        respond contactRiskWarningDispositionTypeList, model: [contactRiskWarningDispositionTypeCount: contactRiskWarningDispositionTypeCount[0], params: params], view: 'index'
    }

    def show(ContactRiskWarningDispositionType contactRiskWarningDispositionType) {
        respond contactRiskWarningDispositionType
    }

    def create() {
        respond new ContactRiskWarningDispositionType(params)
    }

    @Transactional
    def save(ContactRiskWarningDispositionType contactRiskWarningDispositionType) {
        if (contactRiskWarningDispositionType == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (contactRiskWarningDispositionType.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond contactRiskWarningDispositionType.errors, view:'create'
            return
        }

        contactRiskWarningDispositionType.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'contactRiskWarningDispositionType.label', default: 'ContactRiskWarningDispositionType'), contactRiskWarningDispositionType.id])
                redirect contactRiskWarningDispositionType
            }
            '*' { respond contactRiskWarningDispositionType, [status: CREATED] }
        }
    }

    def edit(ContactRiskWarningDispositionType contactRiskWarningDispositionType) {
        respond contactRiskWarningDispositionType
    }

    @Transactional
    def update(ContactRiskWarningDispositionType contactRiskWarningDispositionType) {
        if (contactRiskWarningDispositionType == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (contactRiskWarningDispositionType.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond contactRiskWarningDispositionType.errors, view:'edit'
            return
        }

        contactRiskWarningDispositionType.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'contactRiskWarningDispositionType.label', default: 'ContactRiskWarningDispositionType'), contactRiskWarningDispositionType.id])
                redirect contactRiskWarningDispositionType
            }
            '*'{ respond contactRiskWarningDispositionType, [status: OK] }
        }
    }

    @Transactional
    def delete(ContactRiskWarningDispositionType contactRiskWarningDispositionType) {

        if (contactRiskWarningDispositionType == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        contactRiskWarningDispositionType.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'contactRiskWarningDispositionType.label', default: 'ContactRiskWarningDispositionType'), contactRiskWarningDispositionType.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'contactRiskWarningDispositionType.label', default: 'ContactRiskWarningDispositionType'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
